package org.linlinjava.litemall.wx.manage;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.notify.AwsNotifyService;
import org.linlinjava.litemall.core.notify.NoticeHelper;
import org.linlinjava.litemall.core.util.RegexUtil;
import org.linlinjava.litemall.db.beans.Constants;
import org.linlinjava.litemall.db.domain.LitemallGiftCard;
import org.linlinjava.litemall.db.domain.LitemallGiftCardOrder;
import org.linlinjava.litemall.db.domain.LitemallGiftCardUser;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallGiftCardService;
import org.linlinjava.litemall.wx.constant.LitemallWXConstants;
import org.linlinjava.litemall.wx.util.WxResponseCode;
import org.springframework.stereotype.Service;
import static org.linlinjava.litemall.wx.util.WxResponseCode.MOBILE_CODE_ONE;
/**
 * wx礼物卡管理服务
 *
 * @author yliyun
 * @date 2022/10/19
 */
@Service
@RequiredArgsConstructor
public class WxGiftCardManageService {

    private final NoticeHelper noticeHelper;

    private final AwsNotifyService awsNotifyService;

    /**
     * 给存在用户发送通知
     * @param card
     * @param payUser
     * @param giveeUser
     * @param order
     * @param createCardUser
     */
    public void sendNotificationToExistUser(LitemallGiftCard card, LitemallUser payUser, LitemallUser giveeUser, LitemallGiftCardOrder order, LitemallGiftCardUser createCardUser,String message){
        message= StrUtil.isNotEmpty(message)?message: LitemallWXConstants.WX_GIFTCARD_DEFAULT_MESSAGE;
         /**TODO 给赠送者发送 系统通知 （有账号的用户）*/
        noticeHelper.noticeUser( Constants.MSG_TYPE_OTHER,LitemallWXConstants.WX_GIFTCARD_USER_TITLE_NOTICEHELPER,
                String.format(LitemallWXConstants.WX_GIFTCARD_USER_NOTICEHELPER, order.getAddUserName()), payUser.getId(),createCardUser);
        awsNotifyService.sendSms(MOBILE_CODE_ONE+payUser.getMobile(),
                String.format(LitemallWXConstants.WX_GIFTCARD_USER_NOTICEHELPER, order.getAddUserName()),Constants.AWS_MESSAGE_TYPE_PROMOTIONAL);

        /**TODO 给被赠送者发送 系统通知、短信、邮箱 （有账号的用户）*/
        if(StringUtils.isNotBlank(giveeUser.getEmail())){
            awsNotifyService.noticeMail(String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_TITLE_NOTICEMAIL,order.getAddUserName(),order.getAddUserId()),
                    String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_NOTICEMAIL,order.getAmount(),payUser.getNickname(),payUser.getUsername(),LitemallWXConstants.WX_GIFTCARD_GETTHELINK,payUser.getUsername(),message)
                    ,null,giveeUser.getEmail());
        }else {
            awsNotifyService.sendSms(MOBILE_CODE_ONE+giveeUser.getMobile(),
                    String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_SENDSMS,order.getAmount(),payUser.getNickname(),payUser.getUsername(),LitemallWXConstants.WX_GIFTCARD_GETTHELINK,payUser.getUsername(),message)
                    ,Constants.AWS_MESSAGE_TYPE_PROMOTIONAL);
        }
        noticeHelper.noticeUser( Constants.MSG_TYPE_OTHER,LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_TITLE_NOTICEHELPER,
                String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_NOTICEHELPER,card.getAmount(),payUser.getNickname(),payUser.getNickname(),payUser.getUsername(),message),giveeUser.getId(),card);
    }

    /**
     * 给不存在用户发送通知
     * @param card
     * @param order
     * @param createCardUser
     * @param payUser
     * @param giveeUser
     */
    public void sendNotificationToNotExistUser(LitemallGiftCard card, LitemallGiftCardOrder order, LitemallGiftCardUser createCardUser, LitemallUser payUser, String giveeUser,String message){
        message= StrUtil.isNotEmpty(message)?message: LitemallWXConstants.WX_GIFTCARD_DEFAULT_MESSAGE;
        /**TODO 给赠送者发送 系统通知 （没有账号的用户）*/
        noticeHelper.noticeUser( Constants.MSG_TYPE_OTHER,LitemallWXConstants.WX_GIFTCARD_USER_TITLE_NOTICEHELPER,
                String.format(LitemallWXConstants.WX_GIFTCARD_USER_NOTICEHELPER,order.getAddUserName()), payUser.getId(),createCardUser);
        awsNotifyService.sendSms(MOBILE_CODE_ONE+payUser.getMobile(),
                String.format(LitemallWXConstants.WX_GIFTCARD_USER_NOTICEHELPER,order.getAddUserName()),Constants.AWS_MESSAGE_TYPE_PROMOTIONAL);

        /**TODO 给被赠送者发送 短信、邮箱 （没有账号的用户）正确的情况下才会发送*/
        if(RegexUtil.isEmailExact(giveeUser)){
            awsNotifyService.noticeMail(String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_TITLE_NOTICEMAIL,payUser.getNickname(),payUser.getUsername()),
                    String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_NOTICEMAIL,order.getAmount(),payUser.getNickname(),payUser.getUsername(),LitemallWXConstants.WX_GIFTCARD_DOWNLOADLINK,payUser.getUsername(),message)
                    , String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_NOTICEHELPER,card.getName(),payUser.getMobile(),payUser.getNickname(),payUser.getUsername(),message),giveeUser);
        }
        if(RegexUtil.isMobileValid(giveeUser)){
            awsNotifyService.sendSms(MOBILE_CODE_ONE+giveeUser,
                    String.format(LitemallWXConstants.WX_GIFTCARD_GIVEEUSER_SENDSMS,order.getAmount(),payUser.getNickname(),payUser.getUsername(),LitemallWXConstants.WX_GIFTCARD_GETTHELINK,payUser.getUsername(),message)
                    ,Constants.AWS_MESSAGE_TYPE_PROMOTIONAL);
        }
    }


}
